package cn.zhonghuanzhiyuan.sprc.basicinfo.service;

import cn.org.bachelor.iam.IamContext;
import cn.zhonghuanzhiyuan.sprc.basicinfo.dao.ThirdUnitEmployeeMapper;
import cn.zhonghuanzhiyuan.sprc.basicinfo.domain.ThirdUnitEmployee;
import cn.zhonghuanzhiyuan.sprc.basicinfo.pojo.vo.*;
import cn.zhonghuanzhiyuan.sprc.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import cn.hutool.core.util.ArrayUtil;

import java.util.List;

/**
 * 第三方单位人员(ThirdUnitEmployee)对象服务实现类
 *
 * @author david
 * @since 2024-02-27 18:53:52
 */
@Slf4j
@Service
public class ThirdUnitEmployeeService implements CommonPropertyService {

    @Autowired
    private ThirdUnitEmployeeMapper thirdUnitEmployeeMapper;

    @Autowired
    private IamContext iamContext;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public ThirdUnitEmployee getThirdUnitEmployeeById(String id) {
        return thirdUnitEmployeeMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过实体作为筛选条件实现模糊查询
     *
     * @param queryVo 查询对象
     * @return 对象列表
     */
    public List<ThirdUnitEmployee> selectThirdUnitEmployeeList(QueryThirdUnitEmployeeVo queryVo){
        log.info("多条件模糊查询参数：{}", queryVo);

        Example example = new Example(ThirdUnitEmployee.class);
        Example.Criteria criteria = example.createCriteria();

                if (StringUtils.isNotBlank(queryVo.getId()))
            criteria.andLike("id", "%" + queryVo.getId() + "%");
                        if (StringUtils.isNotBlank(queryVo.getThirdUnitId()))
            criteria.andLike("thirdUnitId", "%" + queryVo.getThirdUnitId() + "%");
                        if (StringUtils.isNotBlank(queryVo.getName()))
            criteria.andLike("name", "%" + queryVo.getName() + "%");
                        if (StringUtils.isNotBlank(queryVo.getCode()))
            criteria.andLike("code", "%" + queryVo.getCode() + "%");
                        if (StringUtils.isNotBlank(queryVo.getIdCode()))
            criteria.andLike("idCode", "%" + queryVo.getIdCode() + "%");
                                        if (StringUtils.isNotBlank(queryVo.getStatus()))
            criteria.andLike("status", "%" + queryVo.getStatus() + "%");
                        if (StringUtils.isNotBlank(queryVo.getDescription()))
            criteria.andLike("description", "%" + queryVo.getDescription() + "%");
                        if (StringUtils.isNotBlank(queryVo.getPhone()))
            criteria.andLike("phone", "%" + queryVo.getPhone() + "%");
                        if (StringUtils.isNotBlank(queryVo.getPosition()))
            criteria.andLike("position", "%" + queryVo.getPosition() + "%");
                                        if (StringUtils.isNotBlank(queryVo.getPicture()))
            criteria.andLike("picture", "%" + queryVo.getPicture() + "%");
                        if (StringUtils.isNotBlank(queryVo.getCertificateName()))
            criteria.andLike("certificateName", "%" + queryVo.getCertificateName() + "%");
                        if (StringUtils.isNotBlank(queryVo.getCertificateNumber()))
            criteria.andLike("certificateNumber", "%" + queryVo.getCertificateNumber() + "%");
                        if (StringUtils.isNotBlank(queryVo.getCertificateLevel()))
            criteria.andLike("certificateLevel", "%" + queryVo.getCertificateLevel() + "%");
                                                        if (StringUtils.isNotBlank(queryVo.getParkId()))
            criteria.andLike("parkId", "%" + queryVo.getParkId() + "%");
                        if (StringUtils.isNotBlank(queryVo.getOrgId()))
            criteria.andLike("orgId", "%" + queryVo.getOrgId() + "%");
                                        if (StringUtils.isNotBlank(queryVo.getUpdateUser()))
            criteria.andLike("updateUser", "%" + queryVo.getUpdateUser() + "%");
                                        if (StringUtils.isNotBlank(queryVo.getCreateUser()))
            criteria.andLike("createUser", "%" + queryVo.getCreateUser() + "%");
                                return thirdUnitEmployeeMapper.selectByExample(example);
        }
    /**
     * 新增或修改数据
     *
     * @param updateOrSaveVo 视图对象
     *
     */

    public void insertOrUpdate(UpdateOrSaveThirdUnitEmployeeVo updateOrSaveVo) {
        //通过主键是否有值判断是新增还是修改操作
        if(StringUtil.isEmpty(updateOrSaveVo.getId())){
            copyCreateProperties(updateOrSaveVo,iamContext);
            thirdUnitEmployeeMapper.insertSelective(updateOrSaveVo);
        }else{
            copyUpdateProperties(updateOrSaveVo,iamContext);
            thirdUnitEmployeeMapper.updateByPrimaryKeySelective(updateOrSaveVo);
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 一个主键或者多个主键 "1" or "1,2,3,4"
     *
     */
    public void deleteByIds(String ids) {
        Example example = new Example(ThirdUnitEmployee.class);
        Example.Criteria criteria = example.createCriteria();

        String[] idArray = StringUtils.split(ids, ",");
        if(ArrayUtil.isNotEmpty(idArray)){
            for (String id : idArray) {
                criteria.orEqualTo("id", id);
            }
        }
        thirdUnitEmployeeMapper.deleteByExample(example);
    }
}
